package com.quectel.mobile.config;

import com.quectel.util.client.minio.MinIOClient;
import com.quectel.util.client.redis.RedisClient;
import com.quectel.util.client.snowflake.SnowflakeClient;
import com.quectel.util.kit.MinIOUtils;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.redis.RedisUtils;
import com.quectel.util.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 启动后配置参数
 *
 * @author caolu
 * @email louis.cao@quectel.com
 */
@Configuration
@Slf4j
public class InitConfig implements ApplicationContextAware {


    /**
     * Redis客户端实例化
     *
     * @param redisProperties
     * @param stringRedisTemplate
     * @return
     */
    @Bean
    RedisClient redisClient(RedisProperties redisProperties,
                            StringRedisTemplate stringRedisTemplate) {

        RedisClient defaultClient = new RedisClient(redisProperties, stringRedisTemplate);

        // 帮助类填充默认实例方便静态调用
        RedisUtils.setDefaultClient(defaultClient);
        log.info("Redis initialization completed");

        return defaultClient;
    }

    /**
     * Snowflake客户端实例化
     *
     * @param applicationContext
     * @param stringRedisTemplate
     * @return
     */
    @Bean
    SnowflakeClient snowflakeClient(ApplicationContext applicationContext,
                                    StringRedisTemplate stringRedisTemplate) {
        SnowflakeClient defaultClient =
                new SnowflakeClient(applicationContext.getEnvironment().getProperty("spring.application.name"), stringRedisTemplate);

        // 帮助类填充默认实例方便静态调用
        Snowflake.setDefaultClient(defaultClient);
        log.info("Snowflake initialization completed");

        return defaultClient;
    }

    /**
     * MinIO客户端初始化
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "minio", name = "enable", havingValue = "true")
    MinIOClient minIOClient() {
        MinIOClient defaultClient = new MinIOClient(
                SpringUtils.getConfigValue("minio.internal-endpoint"),
                SpringUtils.getConfigValue("minio.external-endpoint"),
                SpringUtils.getConfigValue("minio.access-key"),
                SpringUtils.getConfigValue("minio.secret-key")
        );

        // 帮助类填充默认实例方便静态调用
        MinIOUtils.setDefaultClient(defaultClient);
        log.info("MinIO initialization completed");

        return defaultClient;
    }

    /**
     * 注入系统上下文
     *
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.setContext(applicationContext);
        log.info("The application context is ready for use");
    }



}
